package com.example.book_libapp;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;

public class DialogBookRating extends Dialog {

	Context context;
	RatingBar ratingbar;
	Button btnOk, btnCancel;
	EditText review;
	boolean stat = true;

	public DialogBookRating(Context context) {

		super(context);
		this.context = context;
	}

	public void setUpdate(String comment, float r) {
		ratingbar.setRating(r);
		review.setText(comment);
		this.stat = false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.rating_dialog);
		setTitle(R.string.bookdetails_label_rating_give);

		ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		review = (EditText) findViewById(R.id.CommentDailog);

		this.ratingbar.setRating(0);

		btnOk.setOnClickListener(new OKListener());
		btnCancel.setOnClickListener(new CancelListener());
	}

	private class OKListener implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			String str = "";

			Log.e("JSON", ""
					+ review.getText().toString() + " " + ratingbar.getRating() );
			
			str = review.getText().toString();
			if (stat) {
				((BookDetails) context).setInsertReview(ratingbar.getRating(),
						str);
			} else {
				((BookDetails) context).setUpdateReview(ratingbar.getRating(),
						str);
			}

			DialogBookRating.this.dismiss();
		}
	}

	private class CancelListener implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			DialogBookRating.this.dismiss();
		}
	}
}
